package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.OptionMapper;
import com.tanhua.dubbo.mappers.QuestionUserLockMapper;
import com.tanhua.dubbo.mappers.QuestionnaireMapper;
import com.tanhua.dubbo.mappers.SoulQuestionMapper;
import com.tanhua.model.domain.Option;
import com.tanhua.model.domain.QuestionUserLock;
import com.tanhua.model.domain.Questionnaire;
import com.tanhua.model.domain.SoulQuestion;
import com.tanhua.model.vo.OptionVo;
import com.tanhua.model.vo.QuestionVo;
import com.tanhua.model.vo.QuestionnaireVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fff
 * @date 2021/12/11 13:20
 **/
@DubboService
public class QuestionnaireApiImpl implements QuestionnaireApi{
    @Autowired
    private QuestionnaireMapper questionnaireMapper;
    @Autowired
    private SoulQuestionMapper soulQuestionMapper;
    @Autowired
    private OptionMapper optionMapper;
    @Autowired
    private QuestionUserLockMapper questionUserLockMapper;




    @Override
    public List find(Long userId) {

        //首先从锁表中查询出该用户的
       // List<QuestionUserLock> questionUserLock = questionUserLockApi.findById(userId);
        //构建查询条件
        LambdaQueryWrapper<QuestionUserLock> qw = new LambdaQueryWrapper<>();
        qw.eq(QuestionUserLock::getUserId, userId);
        // qw.eq(QuestionUserLock::getIsLock, "0");
        List<QuestionUserLock> questionUserLoc = questionUserLockMapper.selectList(qw);


        //判断是否为null
        if (CollUtil.isEmpty(questionUserLoc)) {
            //如果为null的话调用方法进行保存设置
          //  questionUserLockApi.save(userId);

            //向数据库中插入三条数据
            QuestionUserLock questionUserLock = new QuestionUserLock();
            String s = userId.toString();
            int userid = Integer.parseInt(s);
            questionUserLock.setUserId(userid);
            questionUserLock.setIsLock(0);
            questionUserLock.setQuestionnaireId(1);
            questionUserLockMapper.insert(questionUserLock);

            QuestionUserLock questionUserLock1 = new QuestionUserLock();
            questionUserLock1.setUserId(userid);
            questionUserLock1.setIsLock(1);
            questionUserLock1.setQuestionnaireId(2);
            questionUserLockMapper.insert(questionUserLock1);


            QuestionUserLock questionUserLock2 = new QuestionUserLock();

            questionUserLock2.setUserId(userid);
            questionUserLock2.setIsLock(1);
            questionUserLock2.setQuestionnaireId(3);
            questionUserLockMapper.insert(questionUserLock2);


        }





        //查出所有的数据
     //   QueryWrapper<Questionnaire> qw = new QueryWrapper<>();
      //  List<Questionnaire> list = questionnaireMapper.selectList(qw);

        List<QuestionnaireVo> Vos = new ArrayList<>();



        for (QuestionUserLock q : questionUserLoc) {

            QuestionnaireVo init = new QuestionnaireVo();

            //根据问卷id找对应的题目
            String id = q.getQuestionnaireId().toString();

            Questionnaire questionnaire = questionnaireMapper.selectById(id);

            init.setStar(questionnaire.getStar());
            init.setId(questionnaire.getId());
            init.setName(questionnaire.getName());
            init.setCover(questionnaire.getCover());
            init.setLevel(questionnaire.getLevel().toString());
            //设置lock
            init.setIsLock(q.getIsLock());



            QueryWrapper<SoulQuestion> qw1 = new QueryWrapper<>();
            qw1.eq("questionnaire_id",Integer.valueOf(id));
            List<SoulQuestion> questionList = soulQuestionMapper.selectList(qw1);
            List<QuestionVo> questions = new ArrayList<>();
            for (SoulQuestion question : questionList) {



                //根据题目id找对应的选项
                String id1 = question.getId();
                QueryWrapper<Option> qw2 = new QueryWrapper<>();
                qw2.eq("question_id",id1);
                List<Option> options = optionMapper.selectList(qw2);

                List<OptionVo> optionVos = new ArrayList<>();
                for (Option option : options) {
                    OptionVo optionVo = new OptionVo(option.getId(),option.getContent());

                    optionVos.add(optionVo);
                }

                QuestionVo questionVo = QuestionVo.init(question);
                questionVo.setOptions(optionVos);

                questions.add(questionVo);
            }


            init.setQuestions(questions);

            Vos.add(init);

        }



        return Vos;
    }

    @Override
    public Questionnaire findById(String questionnaireId) {



        return  questionnaireMapper.selectById(questionnaireId);
    }
}
